package org.vehicle.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class StringUtil {

	/**
	 * 判断对象obj是否为空
	 * 
	 * @param obj
	 *            需要判断得变量
	 * @return 如果obj为null则返回false,否则返回true
	 */
	public static boolean isNullOrEmpty(Object obj) {
		if (obj == null ? false : true) {
			if (obj.equals(""))
				return false;
			else
				return true;
		} else {
			return false;
		}
	}

	/**
	 * 判断数组是否为空
	 * 
	 * @param objs
	 * @return
	 */
	public static boolean isNullOrEmpty(Object[] objs) {
		return objs != null && objs.length > 0 ? true : false;
	}

	/**
	 * 判断集合是否为空
	 * 
	 * @param list
	 * @return
	 */
	public static boolean isNullOrEmpty(List list) {
		return list != null && list.size() > 0 ? true : false;
	}

	/**
	 * 将date类型转化为字符串 yyyyMMddHHmmss
	 * 
	 * @param date
	 * @return
	 */
	public static String formateJavaDateToString(Date date) {
		String format = "yyyyMMddHHmmss";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	/**
	 * 将date类型转化为字符串 yyyyMMddHHmmss
	 * 
	 * @param date
	 * @return
	 */
	public static Date formateStringToJavaDate(String date) {
		String format = "yyyyMMddHHmmss";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String formateStringToString(Date date) {
		String format = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}
	
	// 汉字转拼音
	public static String getPinYin(String src) {
		char[] t1 = null;
		t1 = src.toCharArray();
		String[] t2 = new String[t1.length];
		HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
		t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		t3.setVCharType(HanyuPinyinVCharType.WITH_V);
		String t4 = "";
		int t0 = t1.length;
		try {
			for (int i = 0; i < t0; i++) {
				if (java.lang.Character.toString(t1[i]).matches(
						"[\\u4E00-\\u9FA5]+")) {
					t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
					t4 += t2[0] + "-";
				} else {
					t4 += java.lang.Character.toString(t1[i]) + "-";
				}
			}
			return t4;
		} catch (BadHanyuPinyinOutputFormatCombination e1) {
			e1.printStackTrace();
		}
		return t4;
	}

	//转换字符串中英文字母的大小写形式
	public static String getPinYinHeadChar(String str) {
		String convert = "";
		for (int j = 0; j < str.length(); j++) {
			char word = str.charAt(j);
			String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
			if (pinyinArray != null) {
				convert += pinyinArray[0].charAt(0);
			} else {
				convert += word;
			}
		}
		return convert;
	}
}
